<template>
  <div class="login-page">
    <!-- 表单 -->
    <validate-form @form-submit="onFormSubmit" ref="formRef">
      <!-- 使用validate-input组件——可定义规则 -->
      <div class="mb-3">
        <label class="form-label">邮箱地址</label>
        <validate-input
          placeholder="请输入邮箱地址"
          :rules="emailRules"
          v-model="emailVal"
          type="text"
          ref="inputRef"
        ></validate-input>
      </div>
      <!-- 密码框 -->
      <div class="mb-3">
        <label class="form-label">密码</label>
        <validate-input
          placeholder="请输入密码"
          :rules="passwordRules"
          type="password"
          v-model="passwordVal"
          ref="inputRef"
        ></validate-input>
      </div>
      <!-- 定义要填充进具名插槽名为submit的区域内的内容 v-slot:submit-->
      <template #submit>
        <span class="btn btn-danger">submit</span>
      </template>
    </validate-form>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref } from "vue";
import { useStore } from "vuex";
import "bootstrap/dist/css/bootstrap.min.css";
// useRouter 是定义路由的一系列行为
import { useRouter } from "vue-router";
// 引入验证表单规则的组件
import ValidateInput, { RulesProp } from "../components/ValidateInput.vue";
// 导入表单组件（整个）
import ValidateForm from "../components/ValidateForm.vue";
import createMessage from "../components/createMessage";

export default defineComponent({
  name: "App",
  components: {
    ValidateInput,
    ValidateForm
  },
  setup(props, context) {
    // inputRef
    const inputRef = ref<any>();
    // emailVal——自定义设置input输入框的默认值
    const emailVal = ref("");
    // 路由 函数的返回结果也是router
    const router = useRouter();
    // vuex
    const store = useStore();

    // emailRules 参照RuleProp泛型来定义
    const emailRules: RulesProp = [
      { type: "required", message: "电子邮件不能为空" },
      { type: "email", message: "请输入正确的电子邮箱格式" }
    ];
    // password——自定义设置input输入框的默认值
    const passwordVal = ref("");
    // passwordRules 参照RuleProp泛型来定义
    const passwordRules: RulesProp = [
      { type: "required", message: "密码不能为空" },
      {
        type: "range",
        min: { message: "你的密码至少包括六位，不能含有空格", length: 6 },
        max: { message: "你的密码最多包括十五位，不能含有空格", length: 15 }
      }
    ];

    // new formRef
    const formRef = ref<any>();
    // 表单验证的结果
    const onFormSubmit = (result: boolean) => {
      // console.log("--"+inputRef.value.validateInput());
      // 自己加：如果是错误的话就清空 --》交互不好
      if (!result) {
        // 调用子组件ValidateForm中的方法clearInputs
        // console.log('??'+formRef.value.clearInputs);
        // formRef.value.clearInputs();
      } else {
        const payload = {
          email: emailVal.value,
          password: passwordVal.value
        };
        store
          .dispatch("loginAndFetch", payload)
          .then(data => {
            // createMessage("登录成功 2秒后跳转首页", "success", 2000);
            createMessage("登录成功", "success", 2000, "跳转到首页");
            setTimeout(() => {
              router.push("/"); // 跳转路由，和router-link中间的代码是一样的 用模板字符串写法或者对象写法都可
            }, 2000);
          })
          .catch(e => {
            console.log(e);
          });
      }
    };
    return {
      emailRules,
      emailVal,
      passwordRules,
      onFormSubmit,
      inputRef,
      passwordVal,
      formRef
    };
  }
});
</script>

<style lang="less" scoped>
.login-page {
  max-width: 600px;
  margin: 40px auto;
}
</style>
